﻿using System.ServiceModel.Activation;
using UFIDA.U9.WSS.ProxyService.Exceptions;
using UFIDA.U9.WSS.ProxyService.Interfaces;
using UFIDA.U9.WSS.ProxyService.Models;
using UFIDA.U9.WSS.ProxyService.Token;

namespace UFIDA.U9.WSS.ProxyService.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AuthService : IAuthService
    {
        /// <summary>
        ///     认证
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public ReturnMessage<TokenReturnData> Authenticate(ContextInfo context)
        {
            if (context == null)
                throw new WSException(ExceptionCodes.ContextInfoNull, "context is null");
            //创建一个token
            return new ReturnMessage<TokenReturnData>(TokenHelper.Instance.CreateToken(context));
        }

        /// <summary>
        ///     刷新Token
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public ReturnMessage<TokenReturnData> RefreshToken(string accessToken, ContextInfo context)
        {
            if (string.IsNullOrEmpty(accessToken))
                throw new WSException(ExceptionCodes.TokenIsInEmpty, "token is empty");
            //刷新Token
            return new ReturnMessage<TokenReturnData>(TokenHelper.Instance.RefreshToken(accessToken, context));
        }
    }
}